package hv.fmw.core.assets {
	import hv.fmw.core.command.Command;
	import hv.fmw.core.command.ICommand;
	import hv.fmw.utils.Config;

	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	/**
	 * @author hvong
	 */
	public class AssetLoaderCmd extends Command implements ICommand {
		
		private var _id:String;
		private var _src:String;
		private var _content:*;
		
		public function AssetLoaderCmd(id:String, src:String) {
			_id = id;
			_src = src;
		}
		
		/*
		 * Function executed when command called
		 */
		public function execute() : void {
			var extension:String = _src.split(".").pop();
			var reg:RegExp = new RegExp("jpg|png|jpeg|gif");
			if(reg.exec(extension)){
				var loader:Loader = new Loader();
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
					loader.load(new URLRequest(Config.getPath() + _src));
			}
		}
		
		/*
		 * Always call super.complete to terminate commands queue
		 */
		override protected function complete(event:Event = null):void {
			_content = event.target.content;
			super.complete();
		}

		public function get id() : String {
			return _id;
		}

		public function get src() : String {
			return _src;
		}

		public function get content() : * {
			return _content;
		}
	}
}
